package com.orangelife.mobile.zoning.activity;

import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.ToastHelper;
import com.orangelife.mobile.zoning.adapter.SelectZoningAdapter;

/**
 * 选择省份
 *
 * @author Administrator
 */
public class SelectZoningActicity extends OrangeLifeBaseActivity {

    private SelectZoningAdapter<Map<String, Object>> adapter;
    private ListView lvzoning;
    private LinearLayout ll;
    private TextView title;
    private Dialog dlgLoading;
    private List<Map<String, Object>> listmap;
    private List<Map<String, Object>> listMapProvice;
    private List<Map<String, Object>> listMapCity;
    private List<Map<String, Object>> listMapArea;
    private int selectType = SelectZoningActicity.WHAT_PROVINCE;
    private String province;
    private String city;
    private ZoningHandler handler;
    /** 只到城市，不选择区 */
    public static final String INTENT_KEY_NO_AREA = "INTENT_KEY_NO_AREA";
    public static final String INTENT_KEY_ZONING = "zoning";
    public static final int RESULT_CODE_ZONING = 1;
    public static final int WHAT_PROVINCE = 1;
    public static final int WHAT_CITY = 2;
    public static final int WHAT_AREA = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_zonning);
        initView();
        findView();
        handler = new ZoningHandler(this);
        selectProvice();
    }

    private void selectProvice() {
        title.setText("选择省份");
        selectType = WHAT_PROVINCE;
        adapter.setType(selectType);
        Map<String, Object> map = new HashMap<>();
        JSONRequest.getInstance().sendRequest(JSONRequest.GET, Constant.ZONING_PRO_CITY_URL, null, handler, WHAT_PROVINCE, HttpUtil.ACTION_URI);
    }

    private void selectCity() {
        title.setText("选择城市");
        selectType = WHAT_CITY;
        adapter.setType(selectType);
        Map<String, Object> map = new HashMap<>();
        map.put("province", province);
        JSONRequest.getInstance().sendRequest(JSONRequest.GET, Constant.ZONING_PRO_CITY_URL, map, handler, 2, HttpUtil.ACTION_URI);
    }

    private void selectArea() {
        title.setText("选择区");
        selectType = WHAT_AREA;
        adapter.setType(selectType);
        Map<String, Object> map = new HashMap<>();
        map.put("province", province);
        map.put("city", city);
        JSONRequest.getInstance().sendRequest(JSONRequest.GET, Constant.ZONING_PRO_CITY_URL, map, handler, 3, HttpUtil.ACTION_URI);
    }

    static class ZoningHandler extends Handler {
        WeakReference<SelectZoningActicity> wrAct;

        ZoningHandler(SelectZoningActicity act) {
            this.wrAct = new WeakReference<>(act);
        }

        @Override
        public void handleMessage(Message msg) {
            SelectZoningActicity act = wrAct.get();
            if (act != null) {
                switch (msg.what) {
                    case 500:
                        ToastHelper.getInstance()._toast(msg.obj.toString());
                        DialogHelper.closeDialog(act.dlgLoading);
                        break;
                    case 1001:
                        ToastHelper.getInstance()._toast(msg.obj.toString());
                        act.isLogin(act.dlgLoading);
                        break;
                    case WHAT_PROVINCE:
                        act.listMapProvice = JsonReponseHandler.getListFromJson(msg.obj.toString(), "position", null, act.adapter, act.dlgLoading);
                        break;
                    case WHAT_CITY:
                        act.listMapCity = JsonReponseHandler.getListFromJson(msg.obj.toString(), "position", null, act.adapter, act.dlgLoading);
                        break;
                    case WHAT_AREA:
                        act.listMapArea = JsonReponseHandler.getListFromJson(msg.obj.toString(), "position", null, act.adapter, act.dlgLoading);
                        break;
                    default:
                        break;
                }
            }
        }

    }


    /**
     * 设置适配器
     */
    private void setAdapter() {

        listmap = new ArrayList<>();
        adapter = new SelectZoningAdapter<>(
                this, listmap, 0);

        lvzoning.setAdapter(adapter);
    }

    private void initView() {
        title = (TextView) findViewById(R.id.tv_title);
        lvzoning = (ListView) findViewById(R.id.lv_zoning);
        ll = (LinearLayout) findViewById(R.id.ll_score);
        dlgLoading = DialogHelper.getInstance().createLoadingDialog(this,
                Constant.LOADING);
        dlgLoading.show();
    }

    private void findView() {
        title.setText("选择省份");
        ll.setVisibility(View.INVISIBLE);
        lvzoning.setOnItemClickListener(itemClickLintener);
        setAdapter();
    }

    /** 选择完成后，跳回原来的界面 */
    private void jumpBack(String area) {
        Intent intent = new Intent();
        Map<String, Object> map = new HashMap<>();
        map.put("province", province);
        map.put("city", city);
        map.put("district", area);
        Bundle bundle = new Bundle();
        bundle.putSerializable(INTENT_KEY_ZONING, (Serializable) map);
        intent.putExtras(bundle);
        setResult(RESULT_CODE_ZONING, intent);
        finish();
    }

    AdapterView.OnItemClickListener itemClickLintener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> aview, View view, int position,
                                long arg3) {
            switch (selectType) {
                case WHAT_PROVINCE:
                    dlgLoading.show();
                    province = listMapProvice.get(position).get("province").toString();
                    selectCity();
                    break;
                case WHAT_CITY:
                    city = listMapCity.get(position).get("city").toString();
                    dlgLoading.show();
                    selectArea();
                    break;
                case WHAT_AREA:
                    String area = listMapArea.get(position).get("district").toString();
                    jumpBack(area);
                    break;
                default:
                    break;
            }


        }
    };


    private void updateAdapter(List<Map<String, Object>> listmap) {
        adapter.setList(listmap);
        adapter.notifyDataSetChanged();
    }

    public void onBackClick(View v) {
        updateData();
    }

    @Override
    public void onBackPressed() {
        updateData();
    }

    private void updateData() {
        switch (selectType) {
            case WHAT_PROVINCE:
                finish();
                break;
            case WHAT_CITY:
                selectType = WHAT_PROVINCE;
                adapter.setType(selectType);
                updateAdapter(listMapProvice);
                break;
            case WHAT_AREA:
                selectType = WHAT_CITY;
                adapter.setType(selectType);
                updateAdapter(listMapCity);
                break;

            default:
                break;
        }
    }
}
